package com.bosssoft.trainee.rbac.controller;

import com.bosssoft.trainee.rbac.entity.dto.MenusDTO;
import com.bosssoft.trainee.rbac.entity.dto.RolesDTO;
import com.bosssoft.trainee.rbac.entity.vo.CommonResult;
import com.bosssoft.trainee.rbac.entity.vo.MenuVO;
import com.bosssoft.trainee.rbac.entity.vo.RoleVO;
import com.bosssoft.trainee.rbac.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @PostMapping("/insert")
    public CommonResult<MenuVO> insertMenu(@RequestBody MenuVO menuVO){
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        int result = menuService.insertRolesMenu(menusDTO);
        if (result > 0) {
            return new CommonResult(200, "插入成功", result);
        } else {
            return new CommonResult(400, "插入失败", null);
        }
    }

    @PostMapping("/update")
    public CommonResult<MenuVO> updateMenu(@RequestBody MenuVO menuVO) {
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        int result = menuService.insertRolesMenu(menusDTO);
        if (result > 0) {
            return new CommonResult(200, "更改成功", result);
        } else {
            return new CommonResult(400, "更改失败", null);
        }
    }

    @PostMapping("/delete")
    public CommonResult<MenuVO> deleteMenu(@RequestBody MenuVO menuVO){
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        if(menusDTO != null){
            Integer id = menusDTO.getMenuId();
            int result = menuService.deleteRolesMenu(id);
            if (result > 0) {
                return new CommonResult(200, "删除成功,id:", id);
            }
        }
            return new CommonResult(400, "删除失败", null);
    }
    @PostMapping("/getMenu")
    public CommonResult<MenuVO> getMenuByRoleId(@RequestBody RoleVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        Integer roleId = rolesDTO.getRoleId();
        if(roleId != null){
            List<MenusDTO> menusDTOList = menuService.getMenuByRoleId(roleId);
            if(menusDTOList != null){
                return new CommonResult(200, "查找成功", menusDTOList);
            }
        }
        return new CommonResult(400, "查找失败",null );
    }
}
